<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--      筛选-->
      <!--      分栏24占满 -->
      <el-col :span="24" :xs="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="回复内容" prop="commentContent">
            <el-input
              v-model="queryParams.commentContent"
              placeholder="请输入关键字"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="评论用户" prop="fromUid">
            <reference title="用户参照"
                       v-model="userOptions"
                       :operation="{value:'userId',label:'userName'}"
                       :component="() => import('@/views/system/user')" />
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-select
              v-model="queryParams.status"
              placeholder="选择状态"
              clearable
            >
              <el-option
                v-for="dict in sys_normal_disable"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="回复的文章" prop="typeId">
            <reference title="文章参照"
                       v-model="articleOptions"
                       :operation="{value:'id',label:'articleTitle'}"
                       :component="() => import('@/views/article/index')" />
          </el-form-item>
          <el-form-item label="回复时间">
            <el-date-picker
              v-model="dateRange"
              value-format="yyyy-MM-dd HH:mm:ss"
              type="datetimerange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
            >删除</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
        </el-row>

        <el-table row-key="id"
                  :highlight-current-row="type == 1"
                  ref="mainTable"
                  v-loading="loading"
                  :data="data"
                  @current-change="handleCurrentChange"
                  @selection-change="handleSelectionChange" >
          <el-table-column :reserve-selection="true"
                           :type="type == 1 ?'index' : 'selection'"
                           width="50"
                           align="center" />
          <el-table-column v-for="(item,index) in columns"
                           v-if="item.visible"
                           :key="index"
                           :label="item.label"
                           :prop="item.prop"
                           :width="item.width"
                           align="center">
            <template slot-scope="scope">
              <span  v-if="item.prop =='createTime'"  >{{ parseTime(scope.row[item.prop]) }}</span>
              <el-switch
                v-else-if="item.prop == 'status'"
                v-model="scope.row[item.prop]"
                :active-value="0"
                :inactive-value="1"
                @change="handleStatusChange(scope.row)"
              ></el-switch>
              <el-switch
                v-else-if="item.prop == 'isComment'"
                v-model="scope.row[item.prop]"
                :active-value="1"
                :inactive-value="0"
                @change="handleStatusChange(scope.row)"
              ></el-switch>
              <span v-else-if="item.prop == 'fromUser'" >
                {{ scope.row[item.prop] && scope.row[item.prop].userName}}
              </span>
              <span v-else-if="item.prop == 'toUser'" >
                {{ scope.row[item.prop] && scope.row[item.prop].userName}}
              </span>
              <span v-else-if="item.prop == 'parentId'" >
                {{ scope.row[item.prop] == 0 ? 1 : 2}}
              </span>
              <span v-else-if="item.prop == 'commentContent'" >
                {{ scope.row[item.prop]}}
<!--                <el-tooltip class="item" effect="dark" :content="scope.row[item.prop]" placement="top-start">-->
<!--                    {{ scope.row[item.prop]}}-->
<!--                </el-tooltip>-->
              </span>
              <span v-else >{{ scope.row[item.prop] }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            width="160"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
              >删除</el-button>
            </template>

          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>

    </el-row>

  </div>
</template>

<script>
import * as api from '@/api/comment'
import reference from  "@/components/Reference/reference"
import Reference from '@/components/Reference'
export default {
  name: "article",
  components: { Reference },
  data() {
    return {
      sys_normal_disable:[
        {
          value:0,
          label:"正常"
        },
        {
          value:1,
          label:"禁用"
        }
      ],
      authList:[
        {
          value:1,
          label:"公开"
        },
        {
          value:2,
          label:"私有"
        },
        {
          value:3,
          label:"草稿"
        }
      ],
      options:[],
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      data: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      multiple:true,
      // 日期范围
      dateRange: [],
      // 表单参数
      form: {},
      //批改表单
      correctionForm:{},
      //选中列表
      selectData:[],
      userList:[],
      //题库id
      problemTypeId:"",
      //用户参照
      userOptions : {},
      //文章参照
      articleOptions : {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fromUid:null,
        commentContent:"",
        typeId:null,
        searchValue:'',
        status: undefined,
      },
      // 列信息
      columns: [
        { label: `id`, visible: false, prop:"id"},
        { label: `评论用户`, visible: true, prop:"fromUser"},
        { label: `评论内容`, visible: true, prop:"commentContent" , width:"200px"},
        { label: `被回复的用户`, visible: true, prop:"toUser" , width:"200px"},
        { label: `状态`, visible: true, prop:"status"},
        { label: `回复时间`, visible: true, prop:"createTime" },
        { label: `修改时间`, visible: false, prop:"updateTime" },
      ],
      // 表单校验
      rules: {
        name: [
          { required: true, message: "分类名称不能为空", trigger: "blur" },
          { min: 1, max: 7, message: '分类名称长度必须介于 1 和 7 之间', trigger: 'blur' }
        ]
      },
    };
  },
  mixins:[reference],
  async created() {
    await this.getList();

  },
  methods: {
    getAuth(auth){
      return this.authList.filter(item => item.value == auth)[0].label
    },
    /** 查询列表 */
    async getList() {
      this.loading = true;
      this.queryParams.fromUid = this.userOptions.userId
      this.queryParams.typeId =  this.articleOptions.id
      const res = await api.list(this.addDateRange(this.queryParams, this.dateRange));
      this.data = res.data.rows;
      this.handleCurrentChangeBugRepair();
      this.total = res.data.total;
      this.loading = false;
    },
    /** 状态修改 */
    handleStatusChange(row) {
      let text = row.status === 1 ? "停用" : "启用";
      this.$modal.confirm('确认要'+ text + '？').then(function() {
        return api.changeStatus(row.id, row.status);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function() {
        row.status = row.status === 0 ? 1 : 0;
      });
    },
    /**  表单重置 */
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        sort: 0,
        status:0,
        remark:undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {}
      this.userOptions = {}
      this.articleOptions = {}
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      let Ids = this.selectData.map(item => item.id);
      if(row && row.id)
        Ids = [row.id]
      this.$modal.confirm('是否确认删除').then(function() {
        return api.del(Ids);
      }).then(() => {
        this.selectData = []
        this.$refs.mainTable.clearSelection()
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
       // 取消按钮
    cancel() {
      this.open = false;
      this.correctionForm = {}
      // this.resetForm("correctionForm");
    },
    /** 批改提交按钮 */
    submitForm() {
      const param = []
      this.selectData.forEach(item => {
        param.push({id:item.id,...this.correctionForm})
      })
      api.correctionProblem(param).then(res => {
        this.$message.success("批改成功！")
        this.open = false;
      })
    },


  }
};
</script>
<style>

</style>
